<template>
    <div class="dashboard-container">
        <div v-show="showFlag">
            <my-search :fields="fieldsList" :baseNumber="11" @onSearch="searchForm" @onReset="resetForm" :selectIsAddAll="false"></my-search>
            <div class="btn">
                <el-button type="primary" size="mini" @click="switchHandle">统计</el-button>
                <el-button type="primary" size="mini" @click="exportDataHandle">导出</el-button>
            </div>
            <div class="table-list">
                <my-table v-loading="loading" ref="myEltable" style="width: 100%" row-key="id" :showsummary="false" :columns="tableColumns" :tableData="tableDatas" :pageNum="queryObj.pageNo" :pageSize="queryObj.pageSize" :height="$store.state.app.windowHeight - 285" max-height="660px" :current="false" @onCurrentChange="changepageNo" @onPageSizeChange="changePageSize" :totalPage="total" highlight-current-row :ifSelect="false" />
            </div>
        </div>
        <div v-show="!showFlag">
            <el-form :model="ruleForm" ref="ruleForm" label-width="20px" class="demo-ruleForm" :inline="true">
                <el-form-item label="">
                    <!-- <el-checkbox-group v-model="ruleForm.type"> -->
                    <el-checkbox label="行业门类" v-model="ruleForm.hyml_mc"></el-checkbox>
                    <el-checkbox label="行业大类" v-model="ruleForm.hydl_mc"></el-checkbox>
                    <el-checkbox label="行业中类" v-model="ruleForm.hyzl_mc"></el-checkbox>
                    <el-checkbox label="行业小类" v-model="ruleForm.hyxl_mc"></el-checkbox>
                    <el-checkbox label="所属地市" v-model="ruleForm.ssds"></el-checkbox>
                    <el-checkbox label="主管税务机关" v-model="ruleForm.zgswjg_mc"></el-checkbox>
                    <!-- </el-checkbox-group> -->
                </el-form-item>
                <el-form-item label="">
                    <el-button type="primary" size="mini" @click="statisticsHandle">统计</el-button>
                    <el-button type="primary" size="mini" @click="resetRuleForm">重置</el-button>
                    <el-button type="primary" size="mini" @click="switchHandle">列表</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="tjloading" :data="tjtableData" style="width: 100%" border :height="$store.state.app.windowHeight - 185">
                <el-table-column type="index" label="排序" width="60" align="center" :index="currIndex">
                </el-table-column>
                <el-table-column prop="SSDS" align="center" show-overflow-tooltip v-if="ruleForm.ssds" label="地市">
                </el-table-column>
                <el-table-column prop="ZGSWJG_MC" align="center" show-overflow-tooltip v-if="ruleForm.zgswjg_mc" label="主管税务机关">
                </el-table-column>
                <el-table-column prop="HYML_MC" align="center" show-overflow-tooltip v-if="ruleForm.hyml_mc" label="行业门类">
                </el-table-column>
                <el-table-column prop="HYDL_MC" align="center" show-overflow-tooltip v-if="ruleForm.hydl_mc" label="行业大类">
                </el-table-column>
                <el-table-column prop="HYZL_MC" align="center" show-overflow-tooltip v-if="ruleForm.hyzl_mc" label="行业中类">
                </el-table-column>
                <el-table-column prop="HYXL_MC" align="center" show-overflow-tooltip v-if="ruleForm.hyxl_mc" label="行业小类">
                </el-table-column>
                <el-table-column prop="SL" align="center" show-overflow-tooltip label="数量">
                </el-table-column>
                <el-table-column prop="PERCENT" align="center" show-overflow-tooltip label="占比"> </el-table-column>
            </el-table>
        </div>
    </div>
</template>

<script>

import { queryPageList, statistics, exportData } from '@/api/cxtj/zdsygl'
import { hylx } from '@/api/common'
import { downloadFile } from "@/utils/utils"
export default {
    name: 'zdsygl-list',

    data() {
        return {
            loading: false,
            tjloading: false,
            showFlag: true,
            fieldsList: [
                {
                    label: '纳税人名称',
                    field: 'nsrmc',
                    type: 'text',
                    value: ''
                },
                {
                    label: '纳税人识别号',
                    field: 'nsrsbh',
                    type: 'text',
                    value: ''
                },
                {
                    label: '纳税人状态',
                    field: 'nsrzt',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: []
                },
                {
                    label: '管理方式',
                    field: 'glfs',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [
                        {
                            value: '按集团管理',
                            label: '按集团管理'
                        },
                        {
                            value: '按法人管理',
                            label: '按法人管理'
                        }

                    ]
                },
                {
                    label: '行业门类',
                    field: 'hyml_mc',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],
                    change: (value) => {
                        this.gethylx(5, "dm_gy_hy", "dlbz='Y' and sjhy_dm = '" + value + "'", "hy_dm")
                    }
                },
                {
                    label: '行业大类',
                    field: 'hydl_mc',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],
                    change: (value) => {
                        this.gethylx(6, "dm_gy_hy", "zlbz='Y' and sjhy_dm = '" + value + "'", "hy_dm")
                    }
                },
                {
                    label: '行业中类',
                    field: 'hyzl_mc',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],
                    change: (value) => {
                        this.gethylx(7, "dm_gy_hy", "xlbz='Y' and sjhy_dm = '" + value + "'", "hy_dm")
                    }
                },
                {
                    label: '行业小类',
                    field: 'hyxl_mc',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],

                },


                {
                    label: '所属地市',
                    field: 'ssds',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],
                    change: (value) => {
                        console.log(value)
                        this.gethylx(9, "DM_GY_SWJG", "SJSWJG_DM =(SELECT SWJG_DM FROM DM_GY_SWJG WHERE XZQHSZ_DM = '" + value + "')", "swjg_dm")
                    }
                },

                {
                    label: '主管税务机关',
                    field: 'zgswjg_dm',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: []
                },


            ],

            tableColumns: [
                {
                    label: '纳税人名称',
                    prop: 'nsrmc',
                    align: 'center',
                    width: 200
                },
                {
                    label: '纳税人识别号',
                    prop: 'nsrsbh',
                    align: 'center',
                    width: 200
                },
                {
                    label: '纳税人状态',
                    prop: 'nsrzt',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '管理类型',
                    prop: 'glfs',
                    align: 'center',
                    minWidth: 120
                },

                {
                    label: '行业门类',
                    prop: 'hyml_mc',
                    align: 'center',
                    width: 200
                },
                {
                    label: '行业大类',
                    prop: 'hydl_mc',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '行业中类',
                    prop: 'hyzl_mc',
                    align: 'center',
                    width: 200
                },
                {
                    label: '行业小类',
                    prop: 'hyxl_mc',
                    align: 'center',
                    width: 200
                },








                {
                    label: '所属地市',
                    prop: 'ssds',
                    align: 'center',
                    minWidth: 80
                },
                {
                    label: '主管税务机关',
                    prop: 'zgswjg_mc',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '企业层级',
                    prop: 'qycj',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '上级纳税人',
                    prop: 'sj_nsrmc',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '上级纳税人识别号',
                    prop: 'sj_nsrsbh',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '法定代表人',
                    prop: 'fddbrxm',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '法定代表人手机号',
                    prop: 'fddbrsjhm',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '财务人员',
                    prop: 'cwfzrxm',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '财务人员手机号',
                    prop: 'cwfzrsjhm',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '办税人员',
                    prop: 'bsrxm',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '办税人员手机号',
                    prop: 'bsrsjhm',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '成立日期',
                    prop: 'djrq',
                    align: 'center',
                    minWidth: 180
                },






            ],

            tableDatas: [],
            ruleForm: {
                hyml_mc: false,
                hydl_mc: false,
                hyzl_mc: false,
                hyxl_mc: false,
                ssds: false,
                zgswjg_mc: false
            },
            tjtableData: [],
            queryObj: {
                nsrmc: '', //公司名称
                nsrsbh: '',
                nsrzt: '',
                glfs: '',
                hyml_mc: '',
                hydl_mc: '',
                hyzl_mc: '',
                hyxl_mc: '',
                ssds: '',
                zgswjg_dm: '',
                pageNo: 1,
                pageSize: 20
            },
            total: 0,

        }
    },

    mounted() {
        this.gethylx(2, 'DM_GY_NSRZT', "YXBZ = 'Y'", "NSRZT_DM")  //纳税人状态
        this.gethylx(4, 'dm_gy_hy', "mlbz = 'Y'", "hy_dm")  //获取行业门类

        this.gethylx(8, 'DIM_XZQH', "SJXZQH_DM = '130000' and XZQHJC = '2'", "XZQH_DM")  //获取城市
    },
    methods: {

        searchForm(params) {
            this.queryObj.nsrmc = params.nsrmc || ''
            this.queryObj.nsrsbh = params.nsrsbh || ''
            this.queryObj.nsrzt = params.nsrzt || ''
            this.queryObj.glfs = params.glfs || ''
            this.queryObj.hyml_mc = params.hyml_mc ? this.checkParams(4, params.hyml_mc) : ''
            this.queryObj.hydl_mc = params.hydl_mc ? this.checkParams(5, params.hydl_mc) : ''
            this.queryObj.hyzl_mc = params.hyzl_mc ? this.checkParams(6, params.hyzl_mc) : ''
            this.queryObj.hyxl_mc = params.hyxl_mc ? this.checkParams(7, params.hyxl_mc) : ''
            this.queryObj.ssds = params.ssds ? this.checkParams(8, params.ssds) : ''
            this.queryObj.zgswjg_dm = params.zgswjg_dm || ''

            this.getTableData();
        },
        checkParams(index, str) {
            let arrList = this.fieldsList[index].options
            for (let i = 0; i < arrList.length; i++) {
                if (arrList[i].value == str) {
                    return arrList[i].label
                }
            }
        },
        resetForm() {
            for (let key in this.queryObj) {
                if (key == 'pageNo') {
                    this.queryObj[key] = 1;
                } else if (key == 'pageSize') {
                    this.queryObj[key] = 20;
                } else {
                    this.queryObj[key] = '';
                }
            }
            // this.tableDatas = []
            // this.total = 0

        },
        getTableData() {
            this.loading = true
            let params = {
                nsrmc: this.queryObj.nsrmc,
                nsrsbh: this.queryObj.nsrsbh,
                nsrzt: this.queryObj.nsrzt,
                glfs: this.queryObj.glfs,
                hyml_mc: this.queryObj.hyml_mc,
                hydl_mc: this.queryObj.hydl_mc,
                hyzl_mc: this.queryObj.hyzl_mc,
                hyxl_mc: this.queryObj.hyxl_mc,
                ssds: this.queryObj.ssds,
                zgswjg_dm: this.queryObj.zgswjg_dm,
                qylx_dm: 2,

                pageNum: this.queryObj.pageNo,
                pageSize: this.queryObj.pageSize,

            }
            queryPageList(params).then(res => {
                this.loading = false
                if (res.code == 200) {
                    this.tableDatas = res.data.records
                    this.total = res.data.total
                }
            })
        },
        gethylx(index, tableName, where, orderBy) {
            hylx({
                "tableName": tableName,
                "where": where,
                "orderBy": orderBy
                // "tableName": "dm_gy_hy",
                // "where": " mlbz = 'Y'",
                // "orderBy": "hy_dm"
            }).then(res => {
                console.log(res)

                if (res.data.length > 0) {
                    if (index == 2) {
                        this.fieldsList[index].options = res.data.map(item => {
                            return { value: item.NSRZTMC, label: item.NSRZTMC }
                        })
                    } else if (index == 8) {
                        this.fieldsList[index].options = res.data.map(item => {
                            return { value: item.XZQH_DM, label: item.XZQHMC }
                        })
                    } else if (index == 9) {
                        this.fieldsList[index].options = res.data.map(item => {
                            return { value: item.SWJG_DM, label: item.SWJGMC }
                        })
                    } else {
                        this.fieldsList[index].options = res.data.map(item => {
                            return { value: item.HY_DM, label: item.HYMC }
                        })
                    }
                }


            })

        },

      



        changepageNo(currPage) {
            this.queryObj.pageNo = currPage;
            this.getTableData();
        },
        changePageSize(pageSize) {
            this.queryObj.pageSize = pageSize;
            this.getTableData();
        },
        statisticsHandle() {
            this.tjloading = true
            let condition = []
            for (let i in this.ruleForm) {

                if (this.ruleForm[i]) {
                    condition.push(i)
                }
            }

            if (condition.length <= 0) {
                this.$message.error('请选择统计类型！')
                 this.tjloading = false
                return false
            }
            // let condition = []
            let params = {
                condition: condition.join(','),
                qylx_dm: 2,
                sfzb: 'Y'
            }
            console.log(params)

            statistics(params).then(res => {
                 this.tjloading = false
                this.tjtableData = res.data
                console.log(res)
            })
        },
        resetRuleForm() {
            for (let key in this.ruleForm) {
                this.ruleForm[key] = false;
            }
            this.tjtableData = []
        },
        switchHandle() {
            this.showFlag = !this.showFlag
            for (let key in this.ruleForm) {
                this.ruleForm[key] = false;
            }
            this.tjtableData = []
        },
         exportDataHandle() {
            let params = {
                nsrmc: this.queryObj.nsrmc,
                nsrsbh: this.queryObj.nsrsbh,
                nsrzt: this.queryObj.nsrzt,
                glfs: this.queryObj.glfs,
                hyml_mc: this.queryObj.hyml_mc,
                hydl_mc: this.queryObj.hydl_mc,
                hyzl_mc: this.queryObj.hyzl_mc,
                hyxl_mc: this.queryObj.hyxl_mc,
                ssds: this.queryObj.ssds,
                zgswjg_dm: this.queryObj.zgswjg_dm,
                qylx_dm: 2,
                sfzb: 'N'
            }
            exportData(params).then(res => {




                const elink = document.createElement("a");
                elink.download = localStorage.getItem('jmFileName') || '省级列名企业名单.xlsx';
                elink.style.display = "none";
                elink.href = URL.createObjectURL(res);
                document.body.appendChild(elink);
                elink.click();
                URL.revokeObjectURL(elink.href); // 释放URL 对象
                document.body.removeChild(elink);
            })

        },
        currIndex(index) {
            return index + 1;
        },
    }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
    width: 100%;
    min-height: 100%;
    background-color: #fff;
    padding: 8px;
    .btn {
        padding: 0 0 8px 0;
    }
}
// .container {
//     height: 100%;
//     width: 100%;
//     background-color: #fff;
//     .searchMy {
//         padding: 8px;
//         .searchMy-form {
//             height: 28px;
//         }
//     }
// }
</style>
